<?php


namespace App\Admin\Controllers;


use App\Constants\AdvConfigConstant;
use App\Constants\ContentReCommendConstant;
use App\Constants\JumpTypeConstant;
use App\Constants\MiniProgramConstant;
use App\Constants\SignConstant;
use App\Constants\TemplateMessageConstant;
use App\Models\MemberTask;
use App\Models\Settings;
use App\Models\TemplateMessage;
use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class MiniProgramSettingController extends AdminController
{
    protected $tab = [
        'secret'    => '密钥设置',
        'adv'       => '广告设置',
        'task'      => '每日任务',
        'sign'      => '签到设置',
        'message'   => '消息设置',
        'other'     => '其他设置',
        'recommend' => '推荐算法',
    ];

    public function index(Content $content)
    {
        $content->row(function (Row $row) {
            $slug = request()->input('slug');

            $tab = new Tab();

            foreach ($this->tab as $key => $item) {
                $tab->add($item, $this->{$key}($slug));
            }

            $row->column(12, $tab->withCard());
        });

        return $content->header('参数配置')->breadcrumb(
            ['text' => '小程序管理', 'url' => admin_route('mini_program.index')],
            ['text' => '小程序参数配置'],
        );
    }

    protected function secret($slug)
    {
        $form = new Form();

        $form->action(admin_route('mini_program_setting.store'));

        $settings = getMiniSettings($slug, MiniProgramConstant::GROUP);

        $form->text('app_id')->required()->value($settings[MiniProgramConstant::APP_ID] ?? '');
        $form->text('secret')->required()->value($settings[MiniProgramConstant::APP_SECRET] ?? '');
        $form->text('token')->value($settings[MiniProgramConstant::APP_TOKEN] ?? '');
        $form->text('aes_key')->value($settings[MiniProgramConstant::APP_AES_KEY] ?? '');
        $form->text('mch_id', '商户号')->value($settings[MiniProgramConstant::APP_MCH_ID] ?? '');
        $form->text('key', '支付密钥')->value($settings[MiniProgramConstant::APP_KEY] ?? '');
        $form->file('cert_path', '客户端证书')->value($settings[MiniProgramConstant::APP_CERT_PATH] ?? '')->url('upload/pay_cert')->autoUpload();
        $form->file('key_path', '客户端秘钥')->value($settings[MiniProgramConstant::APP_KEY_PATH] ?? '')->url('upload/pay_cert')->autoUpload();

        $form->hidden('slug')->value($slug);
        $form->hidden('mini_setting_type')->value('secret');

        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    protected function adv($slug)
    {
        $form = new Form();

        $form->action(admin_route('mini_program_setting.store'));

        $settings = getMiniSettings($slug, AdvConfigConstant::ADV_GROUP);

        $form->block(12, function (Form\BlockForm $form) use ($settings) {
            $form->title('广告位设置');
            $form->switch(AdvConfigConstant::TOPIC_VIDEO_ADV_STATUS, '帖子视频广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::TOPIC_VIDEO_ADV_STATUS));
            $form->number(AdvConfigConstant::TOPIC_VIDEO_ADV_INTERVAL, '隔N条内容显示一次广告')->value(Arr::get($settings, AdvConfigConstant::TOPIC_VIDEO_ADV_INTERVAL));
            $form->divider();
            $form->switch(AdvConfigConstant::INDEX_VIDEO_ADV_STATUS, '首页视频广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::INDEX_VIDEO_ADV_STATUS));
            $form->number(AdvConfigConstant::INDEX_VIDEO_ADV_INTERVAL, '隔N条内容显示一次广告')->value(Arr::get($settings, AdvConfigConstant::INDEX_VIDEO_ADV_INTERVAL));
            $form->divider();
            $form->switch(AdvConfigConstant::COMMENT_VIDEO_ADV_STATUS, '评论视频广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::COMMENT_VIDEO_ADV_STATUS));
            $form->number(AdvConfigConstant::COMMENT_VIDEO_ADV_INTERVAL, '隔N条内容显示一次广告')->value(Arr::get($settings, AdvConfigConstant::COMMENT_VIDEO_ADV_INTERVAL));
            $form->divider();
            $form->switch(AdvConfigConstant::THEME_VIDEO_ADV_STATUS, '话题视频广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::THEME_VIDEO_ADV_STATUS));
            $form->number(AdvConfigConstant::THEME_VIDEO_ADV_INTERVAL, '隔N条内容显示一次广告')->value(Arr::get($settings, AdvConfigConstant::THEME_VIDEO_ADV_INTERVAL));
            $form->divider();
            $form->switch(AdvConfigConstant::WATERFALL_FLOW_STATUS, '瀑布流原生模板视频广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::WATERFALL_FLOW_STATUS));
            $form->number(AdvConfigConstant::WATERFALL_FLOW_INTERVAL, '隔N条内容显示一次广告')->value(Arr::get($settings, AdvConfigConstant::WATERFALL_FLOW_INTERVAL));
            $form->divider();
            $form->switch(AdvConfigConstant::MY_BANNER_ADV_STATUS, '个人中心banner广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::MY_BANNER_ADV_STATUS));
            $form->radio(AdvConfigConstant::MY_BANNER_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::MY_BANNER_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::INTEGRAL_LOG_SCREEN_ADV_STATUS, '积分明细插屏广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::INTEGRAL_LOG_SCREEN_ADV_STATUS));
            $form->radio(AdvConfigConstant::INTEGRAL_LOG_SCREEN_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::INTEGRAL_LOG_SCREEN_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::INTEGRAL_TASK_BANNER_ADV_STATUS, '积分任务banner广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::INTEGRAL_TASK_BANNER_ADV_STATUS));
            $form->radio(AdvConfigConstant::INTEGRAL_TASK_BANNER_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::INTEGRAL_TASK_BANNER_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::SIGN_VIDEO_ADV_STATUS, '签到激励视频广告')->default(0)->value(Arr::get($settings, AdvConfigConstant::SIGN_VIDEO_ADV_STATUS));
            $form->radio(AdvConfigConstant::SIGN_VIDEO_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::SIGN_VIDEO_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::NEWS_DETAILS_ADV_STATUS, '新闻中心内页广告开关')->default(0)->value(Arr::get($settings, AdvConfigConstant::NEWS_DETAILS_ADV_STATUS));
            $form->radio(AdvConfigConstant::NEWS_DETAILS_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::NEWS_DETAILS_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::DISEASE_DISCERN_DETAILS_ADV_STATUS, '病症鉴别内页广告开关')->default(0)->value(Arr::get($settings, AdvConfigConstant::DISEASE_DISCERN_DETAILS_ADV_STATUS));
            $form->radio(AdvConfigConstant::DISEASE_DISCERN_DETAILS_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::DISEASE_DISCERN_DETAILS_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::PATCH_VIDEO_ADV_STATUS, '穴位视频贴片广告开关')->default(0)->value(Arr::get($settings, AdvConfigConstant::PATCH_VIDEO_ADV_STATUS));
            $form->radio(AdvConfigConstant::PATCH_VIDEO_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::PATCH_VIDEO_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::XW_INFO_ADV_STATUS, '穴位内页广告开关')->default(0)->value(Arr::get($settings, AdvConfigConstant::XW_INFO_ADV_STATUS));
            $form->radio(AdvConfigConstant::XW_INFO_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::XW_INFO_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::TOPIC_INFO_ADV_STATUS, '贴吧内页广告开关')->default(0)->value(Arr::get($settings, AdvConfigConstant::TOPIC_INFO_ADV_STATUS));
            $form->radio(AdvConfigConstant::TOPIC_INFO_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::TOPIC_INFO_ADV_TYPE));
            $form->divider();
            $form->switch(AdvConfigConstant::TCM_ADV_STATUS, '中医百科广告开关')->default(0)->value(Arr::get($settings, AdvConfigConstant::TCM_ADV_STATUS));
            $form->radio(AdvConfigConstant::TCM_ADV_TYPE, '广告类型')->options(['标准', '原生', '随机'])->value(Arr::get($settings, AdvConfigConstant::TCM_ADV_TYPE));
            $form->divider();
        });

        $form->block(12, function (Form\BlockForm $form) use ($settings) {
            $form->title('参数设置');

            $form->text(AdvConfigConstant::BANNER_ADV_ID, 'Banner广告')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::BANNER_ADV_ID));
            $form->text(AdvConfigConstant::INCENTIVE_ADV_ID, '激励广告')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::INCENTIVE_ADV_ID));
            $form->text(AdvConfigConstant::SCREEN_ADV_ID, '插屏广告')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::SCREEN_ADV_ID));
            $form->text(AdvConfigConstant::VIDEO_ADV_ID, '视频广告')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::VIDEO_ADV_ID));
            $form->text(AdvConfigConstant::NATIVE_TEMPLE_ADV_ID, '原生竖板卡片9比16')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::NATIVE_TEMPLE_ADV_ID));
            $form->text(AdvConfigConstant::NATIVE_SCREEN_TEMPLE_ADV_ID, '原生全屏9比16')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::NATIVE_SCREEN_TEMPLE_ADV_ID));
            $form->text(AdvConfigConstant::NATIVE_LATTICE_TEMPLE_ADV_ID, '原生格子')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::NATIVE_LATTICE_TEMPLE_ADV_ID));
            $form->text(AdvConfigConstant::NATIVE_BANNER_TEMPLE_ADV_ID, '原生横版条幅16比9')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::NATIVE_BANNER_TEMPLE_ADV_ID));
            $form->text(AdvConfigConstant::NATIVE_CARD_TEMPLE_ADV_ID, '原生横板卡片16比9')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::NATIVE_CARD_TEMPLE_ADV_ID));
            $form->text(AdvConfigConstant::PATCH_VIDEO_ADV_ID, '视频贴片广告')->help('小程序广告位ID')->value(Arr::get($settings, AdvConfigConstant::PATCH_VIDEO_ADV_ID));
        });

        $form->block(12, function (Form\BlockForm $form) use ($slug) {
            $form->hidden('slug')->value($slug);
            $form->hidden('mini_setting_type')->value('adv');
            $form->submitButton();
        });

        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    protected function task($slug)
    {
        $form = new Form();

        $form->action(admin_route('mini_program_setting.store'));

        $data = MemberTask::query()->where('mini_type', $slug)->get()->toArray();
        foreach ($data as $item) {
            $form->block(6, function (Form\BlockForm $form) use ($item) {
                $form->title($item['remark']);
                $form->switch("{$item['slug']}.status", '是否开启')->default(0)->value($item['status']);
                $form->number("{$item['slug']}.max_finish", '可完成次数')->value($item['max_finish']);
                $form->number("{$item['slug']}.integral", '单次积分')->value($item['integral']);
                $form->select("{$item['slug']}.jump_type", '跳转类型')
                    ->when(JumpTypeConstant::JUMP_TO_RELAY, function (Form\BlockForm $form) use ($item) {
                        $form->text("{$item['slug']}.jump_data", '跳转参数')
                            ->help(<<<HTML
                                默认分享首页 <br>
                                参数格式: <br>
                                <span style="margin-left: 20px;">分享首页: index </span><br>
                                <span style="margin-left: 20px;">分享文章: article|文章id 如:article|1 (文章id空则分享文章首页)</span><br>
                                <span style="margin-left: 20px;">分享帖子: topic|帖子id 如:topic|1 (帖子id空则分享帖子首页)</span><br>
HTML
                            )
                            ->value($item['jump_data']);
                    })
                    ->options(JumpTypeConstant::MAP)->value($item['jump_type']);
                $form->number("{$item['slug']}.sort", '排序')->value($item['sort']);
            });
        }

        $form->block(12, function (Form\BlockForm $form) use ($slug) {
            $form->hidden('slug')->value($slug);
            $form->hidden('mini_setting_type')->value('task');
            $form->submitButton();
        });

        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    protected function sign($slug)
    {
        $form = new Form();

        $form->action(admin_route('mini_program_setting.store'));

        $settings = getMiniSettings($slug, SignConstant::GROUP);

        $map = SignConstant::MAP;
        foreach ($map as $key => $item) {
            $form->number($key, $item)->value($settings[$key] ?? '');
        }

        $form->hidden('slug')->value($slug);
        $form->hidden('mini_setting_type')->value('sign');

        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    protected function message($slug)
    {
        $form = new Form();

        $form->action(admin_route('mini_program_setting.store'));

        $list = TemplateMessage::where('mini_type', $slug)->get()->toArray();
        $list = array_combine(array_column($list, 'type'), $list);

        /*
        $form->block(12, function (Form\BlockForm $form) use ($list) {
            $form->title('咨询快报');

            $form->column(8, function (Form\BlockForm $form) use ($list) {
                $form->text('article[template_id]', '快报动态更新提醒')->value($list[TemplateMessageConstant::HOT_ARTICLE_UPDATE]['template_id'] ?? '');
                $form->text('news[template_id]', '新闻动态更新提醒')->value($list[TemplateMessageConstant::NEWS_ARTICLE_UPDATE]['template_id'] ?? '');
                $form->text('topic[template_id]', '圈子动态更新提醒')->value($list[TemplateMessageConstant::TOPIC_UPDATE]['template_id'] ?? '');

                $form->hidden('article[type]')->value(TemplateMessageConstant::HOT_ARTICLE_UPDATE);
                $form->hidden('news[type]')->value(TemplateMessageConstant::NEWS_ARTICLE_UPDATE);
                $form->hidden('topic[type]')->value(TemplateMessageConstant::TOPIC_UPDATE);

                $form->hidden('article[group]')->value(TemplateMessageConstant::ARTICLE_GROUP);
                $form->hidden('news[group]')->value(TemplateMessageConstant::ARTICLE_GROUP);
                $form->hidden('topic[group]')->value(TemplateMessageConstant::ARTICLE_GROUP);
            });

            $form->column(4, function (Form\BlockForm $form) use ($list) {
                $form->switch('article[status]', '是否开启')->value($list[TemplateMessageConstant::HOT_ARTICLE_UPDATE]['status'] ?? '');;
                $form->switch('news[status]', '是否开启')->value($list[TemplateMessageConstant::NEWS_ARTICLE_UPDATE]['status'] ?? '');
                $form->switch('topic[status]', '是否开启')->value($list[TemplateMessageConstant::TOPIC_UPDATE]['status'] ?? '');
            });
        });
        */

        $form->block(12, function (Form\BlockForm $form) use ($list) {
            $form->title('营销提醒');

            $form->column(8, function (Form\BlockForm $form) use ($list) {
                $form->text('sigin[template_id]', '每日签到提醒')->value($list[TemplateMessageConstant::SIGN_IN_NOTICE]['template_id'] ?? '');
                $form->text('share[template_id]', '分享邀请提醒')->value($list[TemplateMessageConstant::SHARE_INVITE_NOTICE]['template_id'] ?? '');
                $form->text('integral[template_id]', '积分兑换提醒')->value($list[TemplateMessageConstant::INTEGRAL_EXCHANGE_NOTICE]['template_id'] ?? '');

                $form->hidden('sigin[type]')->value(TemplateMessageConstant::SIGN_IN_NOTICE);
                $form->hidden('share[type]')->value(TemplateMessageConstant::SHARE_INVITE_NOTICE);
                $form->hidden('integral[type]')->value(TemplateMessageConstant::INTEGRAL_EXCHANGE_NOTICE);

                $form->hidden('sigin[group]')->value(TemplateMessageConstant::TASK_GROUP);
                $form->hidden('share[group]')->value(TemplateMessageConstant::TASK_GROUP);
                $form->hidden('integral[group]')->value(TemplateMessageConstant::TASK_GROUP);
            });

            $form->column(4, function (Form\BlockForm $form) use ($list) {
                $form->switch('sigin[status]', '是否开启')->value($list[TemplateMessageConstant::SIGN_IN_NOTICE]['status'] ?? '');
                $form->switch('share[status]', '是否开启')->value($list[TemplateMessageConstant::SHARE_INVITE_NOTICE]['status'] ?? '');
                $form->switch('integral[status]', '是否开启')->value($list[TemplateMessageConstant::INTEGRAL_EXCHANGE_NOTICE]['status'] ?? '');
            });
        });

        $form->block(12, function (Form\BlockForm $form) use ($list) {
            $form->title('vip提醒');

            $form->column(8, function (Form\BlockForm $form) use ($list) {
                $form->text('vip_open[template_id]', 'vip开通通知')->value($list[TemplateMessageConstant::VIP_OPEN_NOTICE]['template_id'] ?? '');
                $form->text('vip_due[template_id]', 'vip即将到期通知')->value($list[TemplateMessageConstant::VIP_DUE_NOTICE]['template_id'] ?? '');
                $form->text('vip_expire[template_id]', 'vip到期通知')->value($list[TemplateMessageConstant::VIP_EXPIRE_NOTICE]['template_id'] ?? '');

                $form->hidden('vip_open[type]')->value(TemplateMessageConstant::VIP_OPEN_NOTICE);
                $form->hidden('vip_due[type]')->value(TemplateMessageConstant::VIP_DUE_NOTICE);
                $form->hidden('vip_expire[type]')->value(TemplateMessageConstant::VIP_EXPIRE_NOTICE);

                $form->hidden('vip_open[group]')->value(TemplateMessageConstant::VIP_GROUP);
                $form->hidden('vip_due[group]')->value(TemplateMessageConstant::VIP_GROUP);
                $form->hidden('vip_expire[group]')->value(TemplateMessageConstant::VIP_GROUP);
            });

            $form->column(4, function (Form\BlockForm $form) use ($list) {
                $form->switch('vip_open[status]', '是否开启')->value($list[TemplateMessageConstant::VIP_OPEN_NOTICE]['status'] ?? '');
                $form->switch('vip_due[status]', '是否开启')->value($list[TemplateMessageConstant::VIP_DUE_NOTICE]['status'] ?? '');
                $form->switch('vip_expire[status]', '是否开启')->value($list[TemplateMessageConstant::VIP_EXPIRE_NOTICE]['status'] ?? '');
            });
        });

        /*
        $form->block(12, function (Form\BlockForm $form) use ($list) {
            $form->title('健康信息');
            $form->column(8, function (Form\BlockForm $form) use ($list) {
                $form->text('health[template_id]', '健康计划提醒')->value($list[TemplateMessageConstant::HEALTH_PLAN_NOTICE]['template_id'] ?? '');

                $form->hidden('health[type]')->value(TemplateMessageConstant::HEALTH_PLAN_NOTICE);
            });

            $form->column(4, function (Form\BlockForm $form) use ($list) {
                $form->switch('health[status]', '是否开启')->value($list[TemplateMessageConstant::HEALTH_PLAN_NOTICE]['status'] ?? '');
            });
        });
        */

        $form->block(12, function (Form\BlockForm $form) use ($slug) {
            $form->hidden('mini_setting_type')->value('message');
            $form->hidden('slug')->value($slug);
            $form->submitButton();
        });

        return $form;
    }

    protected function other($slug)
    {
        $form = new Form();

        $form->action(admin_route('mini_program_setting.store'));

        $settings = getMiniSettings($slug, MiniProgramConstant::GROUP);

//        $form->switch(MiniProgramConstant::VIP_IS_CLOSE_ADV, '是否VIP关闭广告')->value($settings[MiniProgramConstant::VIP_IS_CLOSE_ADV] ?? 0);
        $form->switch(MiniProgramConstant::PUBLISH_TOPIC_SWITCH, '是否开启发布帖子功能')->value($settings[MiniProgramConstant::PUBLISH_TOPIC_SWITCH] ?? 0);
        $form->switch(MiniProgramConstant::EXCHANGE_INTEGRAL_SWITCH, '是否开启兑换积分功能')->value($settings[MiniProgramConstant::EXCHANGE_INTEGRAL_SWITCH] ?? 0);
        $form->switch(MiniProgramConstant::ALL_ARTICLE_SWITCH, '是否开启全局快讯和新闻')->value($settings[MiniProgramConstant::ALL_ARTICLE_SWITCH] ?? 0);
        $form->switch(MiniProgramConstant::ALL_TOPIC_SWITCH, '是否开启全局动态圈')->value($settings[MiniProgramConstant::ALL_TOPIC_SWITCH] ?? 0);
        $form->switch(MiniProgramConstant::VIP_RECHARGE_SWITCH, '是否开启VIP充值入口')->value($settings[MiniProgramConstant::VIP_RECHARGE_SWITCH] ?? 0);
        $form->switch(MiniProgramConstant::KF_SWITCH, '是否开启客服入口')->value($settings[MiniProgramConstant::KF_SWITCH] ?? 0);
        $form->switch(MiniProgramConstant::DISEASE_DISCERN_DIY_FORM_SWITCH, '病症鉴别是否开启自定义表单')->value($settings[MiniProgramConstant::DISEASE_DISCERN_DIY_FORM_SWITCH] ?? 0);
        $form->select(MiniProgramConstant::DISEASE_DISCERN_DIY_FORM_ID, '病症鉴别自定义表单')->options(\App\Models\DiyForm::where('mini_type', $slug)->pluck('name', 'id')->all())->value($settings[MiniProgramConstant::DISEASE_DISCERN_DIY_FORM_ID] ?? '');
        $form->image(MiniProgramConstant::DEFAULT_HEAD_PICTURE, '默认头像')
            ->url('upload/files')
            ->autoUpload()
            ->value($settings[MiniProgramConstant::DEFAULT_HEAD_PICTURE] ?? '');
        $form->hidden('slug')->value($slug);
        $form->hidden('mini_setting_type')->value('other');

        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    protected function recommend($slug)
    {
        $form = new Form();

        $form->action(admin_route('mini_program_setting.store'));

        $settings = getMiniSettings($slug, ContentReCommendConstant::GROUP);

        $form->number(ContentReCommendConstant::HOT_NEWS_TJ_DAY, '热点快讯推荐算法')
            ->min(0)
            ->value($settings[ContentReCommendConstant::HOT_NEWS_TJ_DAY] ?? 0)
            ->help('前N天推荐的热点快讯+时间;前N天没有推荐的就时间');

        $form->number(ContentReCommendConstant::ARTICLE_TJ_DAY, '新闻咨询推荐算法')
            ->min(0)
            ->value($settings[ContentReCommendConstant::ARTICLE_TJ_DAY] ?? 0)
            ->help('前N天推荐的新闻咨询+时间;前N天没有推荐的就时间');

        $form->number(ContentReCommendConstant::TOPIC_TJ_DAY, '帖子热门算法')
            ->min(0)
            ->value($settings[ContentReCommendConstant::TOPIC_TJ_DAY] ?? 0)
            ->help('前N天热门的帖子+时间;前N天没有推荐的就时间');

        $form->number(ContentReCommendConstant::COURSE_TJ_DAY, '课程推荐算法')
            ->min(0)
            ->value($settings[ContentReCommendConstant::COURSE_TJ_DAY] ?? 0)
            ->help('前N天热门的帖子+时间;前N天没有推荐的就时间');

        $form->hidden('slug')->value($slug);
        $form->hidden('mini_setting_type')->value('recommend');


        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    public function store()
    {
        $post = request()->post();
        $type = $post['mini_setting_type'];
        $slug = $post['slug'];

        if (!$type) {
            return JsonResponse::make()->error('参数错误');
        }
        if (!$slug) {
            return JsonResponse::make()->error('参数错误');
        }

        unset($post['mini_setting_type'], $post['slug'], $post['_previous_'], $post['_token']);

        try {
            $method = 'store' . Str::studly($type);
            $this->{$method}($post, $slug);
        } catch (\Throwable $e) {
            return JsonResponse::make()->error('保存失败');
        }

        return JsonResponse::make()->success('保存成功！');
    }

    protected function storeSecret(array $data, string $miniType)
    {
        $group = MiniProgramConstant::GROUP;

        foreach ($data as $key => $item) {

            if (!isset(MiniProgramConstant::SETTING_MAP[$key])) {
                continue;
            }

            Settings::updateOrCreate([
                'group'     => $group,
                'name'      => $key,
                'mini_type' => $miniType,
            ], ['value' => $item]);
        }

        flushMiniSettings($miniType, $group);
    }

    protected function storeAdv(array $data, string $miniType)
    {
        $group = AdvConfigConstant::ADV_GROUP;

        foreach ($data as $key => $item) {
            Settings::updateOrCreate([
                'group'     => $group,
                'name'      => $key,
                'mini_type' => $miniType,
            ], ['value' => $item]);
        }

        flushMiniSettings($miniType, $group);
    }

    protected function storeTask(array $data, string $miniType)
    {
        foreach ($data as $key => $item) {
            MemberTask::updateOrCreate([
                'slug'      => $key,
                'mini_type' => $miniType,
            ], $item);
        }
    }

    protected function storeSign(array $data, string $miniType)
    {
        $group = SignConstant::GROUP;

        foreach ($data as $key => $item) {
            Settings::updateOrCreate([
                'group'     => $group,
                'name'      => $key,
                'mini_type' => $miniType,
            ])->update(['value' => $item]);
        }

        flushMiniSettings($miniType, $group);
    }

    protected function storeMessage(array $data, string $miniType)
    {
        foreach ($data as $item) {
            TemplateMessage::updateOrCreate([
                'mini_type' => $miniType,
                'type'      => $item['type']
            ], $item);
        }
    }

    protected function storeOther(array $data, string $miniType)
    {
        $group = MiniProgramConstant::GROUP;

        $data = Arr::only($data, [
//            MiniProgramConstant::VIP_IS_CLOSE_ADV,
            MiniProgramConstant::PUBLISH_TOPIC_SWITCH,
            MiniProgramConstant::EXCHANGE_INTEGRAL_SWITCH,
            MiniProgramConstant::ALL_ARTICLE_SWITCH,
            MiniProgramConstant::ALL_TOPIC_SWITCH,
            MiniProgramConstant::DEFAULT_HEAD_PICTURE,
            MiniProgramConstant::VIP_RECHARGE_SWITCH,
            MiniProgramConstant::KF_SWITCH,
            MiniProgramConstant::DISEASE_DISCERN_DIY_FORM_SWITCH,
            MiniProgramConstant::DISEASE_DISCERN_DIY_FORM_ID,
        ]);

        foreach ($data as $key => $item) {

            if ($key == MiniProgramConstant::DEFAULT_HEAD_PICTURE) {
                if ($item && !Str::startsWith($item, ['http://', 'https://', '://'])) {
                    $item = Storage::disk(config('admin.upload.disk'))->url($item);
                }
            }

            Settings::updateOrCreate(
                ['group' => $group, 'name' => $key, 'mini_type' => $miniType],
                ['value' => $item]
            );
        }

        flushMiniSettings($miniType, $group);
    }

    protected function storeRecommend(array $data, string $miniType)
    {
        $group = ContentReCommendConstant::GROUP;

        $data = Arr::only($data, [
            ContentReCommendConstant::HOT_NEWS_TJ_DAY,
            ContentReCommendConstant::ARTICLE_TJ_DAY,
            ContentReCommendConstant::TOPIC_TJ_DAY,
            ContentReCommendConstant::COURSE_TJ_DAY,
        ]);

        foreach ($data as $key => $item) {
            Settings::updateOrCreate(
                ['group' => $group, 'name' => $key, 'mini_type' => $miniType],
                ['value' => $item]
            );
        }

        flushMiniSettings($miniType, $group);
    }
}
